<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/11/19
 * Time: 15:37
 */

namespace app\index\table;

use app\index\controller\Login;
use think\Db;
use app\index\service\ExcelService;

class CarTable extends BaseTable
{
    protected static $view = 'V_ASSET_CAR';
    protected static $pk = 'FACILITY_NO';
    protected static $configTable = 'ASSET_CAR_CONFIG';

    static public function table(){
        return Db::table(static::$view);
    }

//    static public function add($data){
//        $data[self::$pk] = static::getNextId();
//        $data['INPUT_USER_ID'] = Login::getUserId();
//        $data['INPUT_TIME'] = date('Y-m-d');
//
//        self::toDate($data,'GET_DATE');
//        self::toDate($data,'REPAIRE_DEADLINE');
//        self::toDate($data,'ENTER_DATE');
//        self::toDate($data,'USE_START_DATE');
//        self::toDate($data,'INPUT_TIME');
//        Db::table(static::$table)->insert($data);
//    }

//    static public function addImport($data){
//        $data['INPUT_USER_ID'] = Login::getUserId();
//        $data['INPUT_TIME'] = date('Y-m-d');
//
//        self::toDate($data,'GET_DATE');
//        self::toDate($data,'REPAIRE_DEADLINE');
//        self::toDate($data,'ENTER_DATE');
//        self::toDate($data,'USE_START_DATE');
//        self::toDate($data,'INPUT_TIME');
//        Db::table(static::$table)->insert($data);
//    }

//    static public function update($where,$data){
//        self::toDate($data,'GET_DATE');
//        self::toDate($data,'REPAIRE_DEADLINE');
//        self::toDate($data,'ENTER_DATE');
//        self::toDate($data,'USE_START_DATE');
//        Db::table(static::$table)->where($where)->update($data);
//    }

//    static public function delete($where){
//        return Db::table(static::$table)->where($where)->delete();
//    }

    public static function getConfigAdd(){
        return Db::table(static::$configTable)->where(['ADD_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigEdit(){
        return Db::table(static::$configTable)->where(['EDIT_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigPrint(){
        return Db::table(static::$configTable)->where(['PRINT_FLAG'=>'1'])->order('ID ASC')->select();
    }

//    public static function getNextId(){
//        $year = date('Y');
//        $numMaxRecord = Db::table(static::$table)
//            ->field('max(SUBSTR('.static::$pk.',5,4)) as NUM')
//            ->where([['SUBSTR('.static::$pk.',1,4)','=',$year]])
//            ->fetchSql(true)
//            ->find();
//        $numMaxRecord = Db::query($numMaxRecord);
//        $num = $numMaxRecord[0]['NUM'];
//        if ($num == NULL) {
//            $num = 0;
//        }
//        $num++;
//        return $year.sprintf("%04d", $num);
//    }

    public static function exportExcel($data,$fileName){

        $fieldList = [
            ['title'=>'资产编号','field'=>'CAR_NO'],
            ['title'=>'资产分类代码','field'=>'ASSET_TYPE'],
            ['title'=>'资产名称','field'=>'CAR_NAME'],
            ['title'=>'原资产编号','field'=>'OLD_CAR_NO'],
            ['title'=>'采购组织形式','field'=>'PURCHASE'],
            ['title'=>'预算项目编号','field'=>'BUDGET_NO'],
            ['title'=>'取得方式','field'=>'GETTYPE'],
            ['title'=>'取得日期','field'=>'GET_DATE'],
            ['title'=>'预计使用年限（月）','field'=>'USE_LIFE'],
            ['title'=>'产地','field'=>'ORIGIN_PLACE'],
            ['title'=>'规格型号','field'=>'SPEC'],
            ['title'=>'品牌','field'=>'CAR_MODEL'],
            ['title'=>'车辆识别号','field'=>'CAR_ID_NO'],
            ['title'=>'车牌号','field'=>'LICENSE_NO'],
            ['title'=>'排气量','field'=>'GAS_NUM'],
            ['title'=>'发动机号','field'=>'ENGINE_NO'],
            ['title'=>'使用性质','field'=>'CAR_USE_PROPERTY'],
            ['title'=>'销售商','field'=>'SELLER'],
            ['title'=>'合同编号','field'=>'CONTRACT_NO'],
            ['title'=>'发票编号','field'=>'INVOICE_NO'],
            ['title'=>'保修截止日期','field'=>'REPAIRE_DEADLINE'],
            ['title'=>'编制情况','field'=>'COMPILE'],
            ['title'=>'入账形式','field'=>'ENTER_TYPE'],
            ['title'=>'数量','field'=>'CAR_NUM'],
            ['title'=>'价值类型','field'=>'VALUETYPE'],
            ['title'=>'价值','field'=>'CAR_FEE'],
            ['title'=>'财政性资金','field'=>'FINANCE_FEE'],
            ['title'=>'非财政性资金','field'=>'NON_FINANCE_FEE'],
            ['title'=>'会计凭证号','field'=>'ACCOUNT_CERT_NO'],
            ['title'=>'财务入账日期','field'=>'ENTER_DATE'],
            ['title'=>'使用状态','field'=>'STATUS'],
            ['title'=>'投入使用日期','field'=>'USE_START_DATE'],
            ['title'=>'使用方向','field'=>'USEDIRECTION'],
            ['title'=>'使用单位','field'=>'COLLEGE_ID'],
            ['title'=>'管理单位','field'=>'MGR_COLLEGE_ID'],
            ['title'=>'使用人','field'=>'USE_NAME'],
            ['title'=>'使用性质','field'=>'CARUSETYPE_ID'],
            ['title'=>'折旧状态','field'=>'DEPRECIATE'],
            ['title'=>'折旧方法','field'=>'DEPRECIATE_WAY'],
            ['title'=>'折旧年限（月）','field'=>'DEPRECIATE_LIFE'],
            ['title'=>'减值准备','field'=>'REDUCE_PRE'],
            ['title'=>'月折旧额','field'=>'DEPRECIATE_PER_MON'],
            ['title'=>'残值率','field'=>'DEPRECIATE_PERCENTAGE'],
            ['title'=>'已提折旧月数','field'=>'DEPRECIATED_MONTH'],
            ['title'=>'累计折旧','field'=>'ACC_DEPRECIATE'],
            ['title'=>'备注','field'=>'REMARK'],
            ['title'=>'隶属单位','field'=>'BELONG'],
            ['title'=>'制单人','field'=>'INPUT_NAME'],
            ['title'=>'制单时间','field'=>'INPUT_TIME'],
        ];

        $excel = new ExcelService();
        $excel->exportExcel($data,$fileName,$fieldList);
    }

    const CAR_FIELD_LIST = [
        "CAR_NO","ASSET_TYPE_ID","CAR_NAME","OLD_CAR_NO","PURCHASE_ID","BUDGET_NO","GETTYPE_ID","GET_DATE","USE_LIFE","ORIGIN_PLACE","SPEC","CAR_MODEL","CAR_ID_NO","LICENSE_NO","GAS_NUM","ENGINE_NO","CAR_USE_PROPERTY","SELLER","CONTRACT_NO","INVOICE_NO","REPAIRE_DEADLINE","COMPILE_ID","ENTER_TYPE_ID","CAR_NUM","VALUETYPE_ID","CAR_FEE","FINANCE_FEE","NON_FINANCE_FEE","ACCOUNT_CERT_NO","ENTER_DATE","STATUS_ID","USE_START_DATE","USEDIRECTION_ID","COLLEGE_ID","MGR_COLLEGE_ID","USE_NAME","CARUSETYPE_ID","DEPRECIATE_STATUS_ID","DEPRECIATE_WAY","DEPRECIATE_LIFE","REDUCE_PRE","DEPRECIATE_PER_MON","DEPRECIATE_PERCENTAGE","DEPRECIATED_MONTH","ACC_DEPRECIATE","REMARK","BELONG_ID"
    ];
}